#include "AudioControl.h"
#include "AudioSource.h"
#include "GameObject.h"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

AudioControl::AudioControl(void)
    : Behaviour(), state(NULL)
{
    type = "AudioControl";
}

AudioControl::AudioControl(GameObject *obj)
    : Behaviour(obj), state(NULL)
{
    type = "AudioControl";
}

AudioControl::~AudioControl(void)
{
    delete state;
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void AudioControl::initialize(void) throw(...)
{
    // Create states for this behaviour
    state = new AudioControlState(this);
    setCurrentState(state);

    // Base class initialization
    Behaviour::initialize();
}

//===========================================================================//

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

AudioControlState::AudioControlState(void)
    : State()
{
}

AudioControlState::AudioControlState(Behaviour *parent)
    : State(parent)
{
}

AudioControlState::~AudioControlState(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void AudioControlState::onLoop(const FLOAT32 time) throw(...)
{
    // Obtain AudioSource component
    AudioSource *audio = dynamic_cast<AudioSource*> 
        ((fsm->getObject())->getComponent("AudioSource"));

    // Enable / Disable music
    if (fsm->keyPressed(KEY_1))
    {
        if (audio->getAudioType() == MUSIC)
        {
            if (audio->getMute())
            {
                audio->setMute(false);
                audio->play();
            }
            else
            {
                audio->setMute(true);
                audio->stop();
            }
        }
    }

    // Enable / Disable sound effect
    else if (fsm->keyPressed(KEY_2))
    {
        if (audio->getAudioType() == EFFECT)
        {
            if (audio->getMute())
            {
                audio->setMute(false);
            }
            else
            {
                audio->setMute(true);
                audio->stop();
            }
        }
    }

    // Music volume up
    else if (fsm->keyPressed(PLUS_KEY))
    {
        if (audio->getAudioType() == MUSIC)
        {
            audio->setVolume(audio->getVolume() + 0.1f);
        }
    }
    // Music volume down
    else if (fsm->keyPressed(MINUS_KEY))
    {
        if (audio->getAudioType() == MUSIC)
        {
            audio->setVolume(audio->getVolume() - 0.1f);
        }
    }

    // Sound effect volume up
    else if (fsm->keyPressed(MULT_KEY))
    {
        if (audio->getAudioType() == EFFECT)
        {
            audio->setVolume(audio->getVolume() + 0.1f);
        }
    }
    // Sound effect volume down
    else if (fsm->keyPressed(DIV_KEY))
    {
        if (audio->getAudioType() == EFFECT)
        {
            audio->setVolume(audio->getVolume() - 0.1f);
        }
    }


}
